package com.example.chuntiao.cophook.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.example.chuntiao.cophook.R;
import com.example.chuntiao.cophook.beans.Person;
import com.example.chuntiao.cophook.mytools.MyApp;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by chuntiao on 17-8-21.
 */

public class PersonHiddenAdapter extends RecyclerView.Adapter {

    public interface OnCancelClickListener {
        void onClick(View v, int position);
    }

    static final int TYPE_NORMAL = 1;
    static final int TYPE_GAP = 2;

    public void setmPersons(ArrayList<Person> mPersons) {
        this.mPersons = mPersons;
    }

    ArrayList<Person> mPersons;
    Context mContext;

    public void setOnCancelClickListener(OnCancelClickListener listener) {
        this.listener = listener;
    }

    private OnCancelClickListener listener;

    public ArrayList<Person> getmPersons() {
        return mPersons;
    }

    public PersonHiddenAdapter() {
        super();
        mPersons = new ArrayList<>();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE_NORMAL:
                View vNormal = LayoutInflater.from(mContext).inflate(R.layout.person_item_hidden, parent, false);
                holder = new NormalHolder(vNormal);
                break;
            case TYPE_GAP:
                View vGap = LayoutInflater.from(mContext).inflate(R.layout.person_item_gap, parent, false);
                holder = new GapHolder(vGap);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

        Person p = mPersons.get(position);
        switch (holder.getItemViewType()) {
            case TYPE_GAP:
                ((GapHolder) holder).textGap.setText(p.getPyOfHomeName());
                break;
            case TYPE_NORMAL:
                NormalHolder normalHolder = (NormalHolder) holder;
                normalHolder.btnCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.onClick(v, position);
                    }
                });
                normalHolder.textName.setText(p.getName());
                normalHolder.textUid.setText(p.getUid());

                if (p.getImgVer() != -1) {
                    Glide.with(mContext).load(MyApp.HOST_URL + "image/" + "u_" + p.getUid())
                            .transition(new DrawableTransitionOptions().crossFade())
                            .apply(RequestOptions.signatureOf(new ObjectKey(p.getImgVer())))
                            .into(normalHolder.imgPerson);
                }

        }
    }

    @Override
    public int getItemCount() {
        return mPersons.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (mPersons.get(position).getUid() == null)
            return TYPE_GAP;
        else
            return TYPE_NORMAL;
    }

    private class NormalHolder extends RecyclerView.ViewHolder {
        CircleImageView imgPerson;
        TextView textName;
        TextView textUid;
        Button btnCancel;

        public NormalHolder(View itemView) {
            super(itemView);
            imgPerson = (CircleImageView) itemView.findViewById(R.id.img_person);
            textName = (TextView) itemView.findViewById(R.id.text_name);
            textUid = (TextView) itemView.findViewById(R.id.text_uid);
            btnCancel = (Button) itemView.findViewById(R.id.btn_cancel);
        }
    }

}
